﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Controls.Maps;
using System.Device.Location;
using System.Windows.Threading;
using System.Windows.Navigation;


namespace Flag
{
    public partial class map : PhoneApplicationPage
    {


        List<Player> players;
        GeoCoordinate redfpos, bluefpos, redbpos, bluebpos = new GeoCoordinate(50,50);
        String user;

        public map()
        {
            InitializeComponent();

            DispatcherTimer timer = new DispatcherTimer();
            timer.Interval = TimeSpan.FromSeconds(5);
            timer.Tick += Update;
            timer.Start();

            players= new List<Player>(); // Lista di player che vengono caricati dal server
        }

        private void buttonRoad_Click(object sender, RoutedEventArgs e)
        {
            map1.Mode = new RoadMode();
        }

        /// Aerial view button event handler
        private void buttonAerial_Click(object sender, RoutedEventArgs e)
        {
            map1.Mode = new AerialMode();
        }

        /// Invia al server la richiesta di aggiornamento e aggiorna la mappa
        private void Update(object sender, EventArgs e)
        {
            //TODO invia al server la richiesta di aggiornamento
            //TODO aggiorna la mappa con i dati ricevuti
            // Posizione Bandiere
            Pushpin redf = new Pushpin();
            Pushpin bluef = new Pushpin();
            // Posizione Basi
            Pushpin redb = new Pushpin();
            Pushpin blueb = new Pushpin();

            redf.Location = redfpos;
            bluef.Location = bluefpos;
            redb.Location = redbpos;
            blueb.Location = bluebpos;
            redf.Style = (Style)(Application.Current.Resources["RedFlag"]);
            bluef.Style = (Style)(Application.Current.Resources["BlueFlag"]);
            redb.Style = (Style)(Application.Current.Resources["RedBase"]);
            blueb.Style = (Style)(Application.Current.Resources["BlueBase"]);

            map1.Children.Add(redf);
            map1.Children.Add(bluef);
            map1.Children.Add(redb);
            map1.Children.Add(blueb);

            foreach (Player player in players)
            {
                Pushpin p = new Pushpin();
                p.Foreground = new SolidColorBrush(Colors.Black);

                if (player.color == Colors.Red) p.Background = new SolidColorBrush(Colors.Red);
                else p.Background = new SolidColorBrush(Colors.Blue);
                p.Content = player.user;
                p.Location = player.position;

                map1.Children.Add(p);
            }
        }

        /// Transfer button event handler
        private void buttonTransfer_Click(object sender, RoutedEventArgs e)
        {
            //TODO invia al server la richiesta di trasferimento bandiera
        }

        /// Zoom out button event handler
        private void buttonZoomOut_Click(object sender, RoutedEventArgs e)
        {
            double zoom;
            zoom = map1.ZoomLevel;
            map1.ZoomLevel = --zoom;
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            // Let base handle
            base.OnNavigatedTo(e);

            // Get the User
            user = NavigationContext.QueryString["User"];
        }

    }
}